/*
 * © Copyright 2011 Romain Marteau, Matthieu Totet
 * 
 *    This file is part of VineaToum.
 *
 *    VineaToum is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    VineaToum is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with VineaToum.  If not, see <http://www.gnu.org/licenses/>.
 *    
 */

package org.vineatoum.view.action;

import javax.swing.JPanel;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.Date;

import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerNumberModel;

import org.vineatoum.business.action.analyse.AnalyseLabo;
import org.vineatoum.model.abstraction.IVineaToumModel;

import com.toedter.calendar.JDateChooser;

public class AnalyseLaboPanel extends JPanel {
	private static final long serialVersionUID = -6779005200522265769L;
	private JDateChooser dateChooserDate;
	private JSpinner spinnerTitreAlcoo;
	private JSpinner spinnerGlucoseFructose;
	private JSpinner spinnerAciditeVolatile;
	private JSpinner spinnerAciditeAcetique;
	private JSpinner spinnerAciditeVolatileCalculee;
	private JSpinner spinnerAciditeTotaleG;
	private JSpinner spinnerAciditeTotaleMeq;
	private JSpinner spinnerPH;
	private JSpinner spinnerSO2libre;
	private JSpinner spinnerSO2Total;
	private JSpinner spinnerAcideMalique;
	private JSpinner spinnerFer;
	private JTextArea textAreaComment;

	/**
	 * Create the panel.
	 * 
	 * @param prelevement
	 * @param model
	 */
	public AnalyseLaboPanel(IVineaToumModel model, AnalyseLabo analyse) {
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 13, 0, 150, 0, 0, 150, 20, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 20, 20, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 1.0, 0.0, 0.0, 1.0, 0.0, 0.0,
				1.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				Double.MIN_VALUE, 0.0, 0.0, 1.0 };
		setLayout(gbl_panel);
		{
			JLabel lblDate = new JLabel("Date");
			GridBagConstraints gbc_lblDate = new GridBagConstraints();
			gbc_lblDate.insets = new Insets(0, 0, 5, 5);
			gbc_lblDate.gridx = 1;
			gbc_lblDate.gridy = 1;
			add(lblDate, gbc_lblDate);
		}
		{
			dateChooserDate = new JDateChooser(analyse.getDate());
			GridBagConstraints gbc_textField = new GridBagConstraints();
			gbc_textField.insets = new Insets(0, 0, 5, 5);
			gbc_textField.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField.gridx = 2;
			gbc_textField.gridy = 1;
			add(dateChooserDate, gbc_textField);
		}
		{
			JLabel lblTitreAlcoo = new JLabel("Titre Alcoom\u00E9trique (% vol)");
			GridBagConstraints gbc_lblTitreAlcoo = new GridBagConstraints();
			gbc_lblTitreAlcoo.insets = new Insets(0, 0, 5, 5);
			gbc_lblTitreAlcoo.gridx = 1;
			gbc_lblTitreAlcoo.gridy = 2;
			add(lblTitreAlcoo, gbc_lblTitreAlcoo);
		}
		{
			spinnerTitreAlcoo = new JSpinner();
			spinnerTitreAlcoo.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerTitreAlcoo.setValue(analyse.getTitre_alcoo());
			GridBagConstraints gbc_textField_1 = new GridBagConstraints();
			gbc_textField_1.insets = new Insets(0, 0, 5, 5);
			gbc_textField_1.fill = GridBagConstraints.HORIZONTAL;
			gbc_textField_1.gridx = 2;
			gbc_textField_1.gridy = 2;
			add(spinnerTitreAlcoo, gbc_textField_1);
		}
		{
			JLabel lblGlucosefructose = new JLabel("Glucose/Fructose (g/l)");
			GridBagConstraints gbc_lblGlucosefructose = new GridBagConstraints();
			gbc_lblGlucosefructose.insets = new Insets(0, 0, 5, 5);
			gbc_lblGlucosefructose.gridx = 4;
			gbc_lblGlucosefructose.gridy = 2;
			add(lblGlucosefructose, gbc_lblGlucosefructose);
		}
		{
			spinnerGlucoseFructose = new JSpinner();
			spinnerGlucoseFructose.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerGlucoseFructose.setValue(analyse.getGluc_fruc());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 5;
			gbc_spinner.gridy = 2;
			add(spinnerGlucoseFructose, gbc_spinner);
		}
		{
			JLabel lblAciditeVolatile = new JLabel("Acidit\u00E9 Volatile (g/l)");
			GridBagConstraints gbc_lblAciditeVolatile = new GridBagConstraints();
			gbc_lblAciditeVolatile.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditeVolatile.gridx = 1;
			gbc_lblAciditeVolatile.gridy = 3;
			add(lblAciditeVolatile, gbc_lblAciditeVolatile);
		}
		{
			spinnerAciditeVolatile = new JSpinner();
			spinnerAciditeVolatile.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerAciditeVolatile.setValue(analyse.getAcidite_volatile());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 2;
			gbc_spinner.gridy = 3;
			add(spinnerAciditeVolatile, gbc_spinner);
		}
		{
			JLabel lblAciditAcetique = new JLabel("Acidit\u00E9 Ac\u00E9tique (g/l)");
			GridBagConstraints gbc_lblAciditActique = new GridBagConstraints();
			gbc_lblAciditActique.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditActique.gridx = 4;
			gbc_lblAciditActique.gridy = 3;
			add(lblAciditAcetique, gbc_lblAciditActique);
		}
		{
			spinnerAciditeAcetique = new JSpinner();
			spinnerAciditeAcetique.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerAciditeAcetique.setValue(analyse.getAcidite_acetique());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 5;
			gbc_spinner.gridy = 3;
			add(spinnerAciditeAcetique, gbc_spinner);
		}
		{
			JLabel lblAciditeVolatileCalculee = new JLabel("Acidit\u00E9 Volatile Calculée (g/l)");
			GridBagConstraints gbc_lblAciditeVolatile = new GridBagConstraints();
			gbc_lblAciditeVolatile.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditeVolatile.gridx = 1;
			gbc_lblAciditeVolatile.gridy = 4;
			add(lblAciditeVolatileCalculee, gbc_lblAciditeVolatile);
		}
		{
			spinnerAciditeVolatileCalculee = new JSpinner();
			spinnerAciditeVolatileCalculee.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerAciditeVolatileCalculee.setValue(analyse.getAcidite_volatile_calculee());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 2;
			gbc_spinner.gridy = 4;
			add(spinnerAciditeVolatileCalculee, gbc_spinner);
		}
		{
			JLabel lblAciditeTotale = new JLabel("Acidit\u00E9 Totale (g/l)");
			GridBagConstraints gbc_lblAciditActique = new GridBagConstraints();
			gbc_lblAciditActique.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditActique.gridx = 4;
			gbc_lblAciditActique.gridy = 4;
			add(lblAciditeTotale, gbc_lblAciditActique);
		}
		{
			spinnerAciditeTotaleG = new JSpinner();
			spinnerAciditeTotaleG.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerAciditeTotaleG.setValue(analyse.getAcidite_totale_g());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 5;
			gbc_spinner.gridy = 4;
			add(spinnerAciditeTotaleG, gbc_spinner);
		}
		{
			JLabel lblAciditeTotale = new JLabel("Acidit\u00E9 Totale (mEq/l)");
			GridBagConstraints gbc_lblAciditeVolatile = new GridBagConstraints();
			gbc_lblAciditeVolatile.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditeVolatile.gridx = 1;
			gbc_lblAciditeVolatile.gridy = 5;
			add(lblAciditeTotale, gbc_lblAciditeVolatile);
		}
		{
			spinnerAciditeTotaleMeq = new JSpinner();
			spinnerAciditeTotaleMeq.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerAciditeTotaleMeq.setValue(analyse.getAcidite_totale_meq());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 2;
			gbc_spinner.gridy = 5;
			add(spinnerAciditeTotaleMeq, gbc_spinner);
		}
		{
			JLabel lblPH = new JLabel("PH");
			GridBagConstraints gbc_lblAciditActique = new GridBagConstraints();
			gbc_lblAciditActique.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditActique.gridx = 4;
			gbc_lblAciditActique.gridy = 5;
			add(lblPH, gbc_lblAciditActique);
		}
		{
			spinnerPH = new JSpinner();
			spinnerPH.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerPH.setValue(analyse.getPh());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 5;
			gbc_spinner.gridy = 5;
			add(spinnerPH, gbc_spinner);
		}
		{
			JLabel lblSO2libre = new JLabel("SO2 libre (mg/l)");
			GridBagConstraints gbc_lblAciditeVolatile = new GridBagConstraints();
			gbc_lblAciditeVolatile.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditeVolatile.gridx = 1;
			gbc_lblAciditeVolatile.gridy = 6;
			add(lblSO2libre, gbc_lblAciditeVolatile);
		}
		{
			spinnerSO2libre = new JSpinner();
			spinnerSO2libre.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerSO2libre.setValue(analyse.getSo2libre());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 2;
			gbc_spinner.gridy = 6;
			add(spinnerSO2libre, gbc_spinner);
		}
		{
			JLabel lblSO2Total = new JLabel("SO2 total (mg/l)");
			GridBagConstraints gbc_lblAciditActique = new GridBagConstraints();
			gbc_lblAciditActique.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditActique.gridx = 4;
			gbc_lblAciditActique.gridy = 6;
			add(lblSO2Total, gbc_lblAciditActique);
		}
		{
			spinnerSO2Total = new JSpinner();
			spinnerSO2Total.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerSO2Total.setValue(analyse.getSo2total());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 5;
			gbc_spinner.gridy = 6;
			add(spinnerSO2Total, gbc_spinner);
		}
		{
			JLabel lblAcideMalique = new JLabel("Acide Malique (g/l)");
			GridBagConstraints gbc_lblAciditeVolatile = new GridBagConstraints();
			gbc_lblAciditeVolatile.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditeVolatile.gridx = 1;
			gbc_lblAciditeVolatile.gridy = 7;
			add(lblAcideMalique, gbc_lblAciditeVolatile);
		}
		{
			spinnerAcideMalique = new JSpinner();
			spinnerAcideMalique.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerAcideMalique.setValue(analyse.getAcide_malique());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 2;
			gbc_spinner.gridy = 7;
			add(spinnerAcideMalique, gbc_spinner);
		}
		{
			JLabel lblFer = new JLabel("Fer (mg/l)");
			GridBagConstraints gbc_lblAciditActique = new GridBagConstraints();
			gbc_lblAciditActique.insets = new Insets(0, 0, 5, 5);
			gbc_lblAciditActique.gridx = 4;
			gbc_lblAciditActique.gridy = 7;
			add(lblFer, gbc_lblAciditActique);
		}
		{
			spinnerFer = new JSpinner();
			spinnerFer.setModel(new SpinnerNumberModel(new Float(0),
					new Float(0), new Float(999999), new Float(1)));
			spinnerFer.setValue(analyse.getFer());
			GridBagConstraints gbc_spinner = new GridBagConstraints();
			gbc_spinner.fill = GridBagConstraints.HORIZONTAL;
			gbc_spinner.insets = new Insets(0, 0, 5, 5);
			gbc_spinner.gridx = 5;
			gbc_spinner.gridy = 7;
			add(spinnerFer, gbc_spinner);
		}
		{
			JLabel lblCommentaire = new JLabel("Commentaire");
			GridBagConstraints gbc_lblCommentaire = new GridBagConstraints();
			gbc_lblCommentaire.insets = new Insets(0, 0, 5, 5);
			gbc_lblCommentaire.gridx = 1;
			gbc_lblCommentaire.gridy = 8;
			add(lblCommentaire, gbc_lblCommentaire);
		}
		{
			textAreaComment = new JTextArea();
			GridBagConstraints gbc_textAreaComment = new GridBagConstraints();
			gbc_textAreaComment.fill = GridBagConstraints.HORIZONTAL;
			gbc_textAreaComment.gridwidth = 4;
			gbc_textAreaComment.insets = new Insets(0, 0, 5, 5);
			gbc_textAreaComment.gridx = 2;
			gbc_textAreaComment.gridy = 8;
			add(textAreaComment, gbc_textAreaComment);
			textAreaComment.setText(analyse.getComment());
			textAreaComment.setRows(5);	
		}
		
	}

	/**
	 * @param dateChooserDate the dateChooserDate to set
	 */
	public void setDate(Date date) {
		this.dateChooserDate.setDate(date);
	}

	/**
	 * @return the dateChooserDate
	 */
	public Date getDate() {
		return dateChooserDate.getDate();
	}
	
	public void setTitreAlcoometrique(float titreAlcoometrique) {
		this.spinnerTitreAlcoo.setValue(titreAlcoometrique);
	}

	public float getTitreAlcoometrique() {
		return (Float) spinnerTitreAlcoo.getValue();
	}

	/**
	 * @param textAreaComment the textAreaComment to set
	 */
	public void setComment(String comment) {
		this.textAreaComment.setText(comment);
	}

	/**
	 * @return the textAreaComment
	 */
	public String getComment() {
		return textAreaComment.getText();
	}
	
	public AnalyseLabo getAnalyseLabo() {
		AnalyseLabo analyse = new AnalyseLabo();
		
		analyse.setDate(dateChooserDate.getDate());
		analyse.setAcide_malique((Float)spinnerAcideMalique.getValue());
		analyse.setAcidite_acetique((Float)spinnerAciditeAcetique.getValue());
		analyse.setAcidite_totale_g((Float)spinnerAciditeTotaleG.getValue());
		analyse.setAcidite_totale_meq((Float)spinnerAciditeTotaleMeq.getValue());
		analyse.setAcidite_volatile((Float)spinnerAciditeVolatile.getValue());
		analyse.setAcidite_volatile_calculee((Float)spinnerAciditeVolatileCalculee.getValue());
		analyse.setComment(textAreaComment.getText());
		analyse.setFer((Float)spinnerFer.getValue());
		analyse.setGluc_fruc((Float)spinnerGlucoseFructose.getValue());
		analyse.setPh((Float)spinnerPH.getValue());
		analyse.setSo2libre((Float)spinnerSO2libre.getValue());
		analyse.setSo2total((Float)spinnerSO2Total.getValue());
		analyse.setTitre_alcoo((Float)spinnerTitreAlcoo.getValue());
		
		return analyse;	
	}

}
